<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">




<script>
    (function(){
        if(''){
            if (prompt('请输入文章密码') !== ''){
                alert('密码错误！');
                history.back();
            }
        }
    })();
  </script>






<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />



  <link rel="icon" type="image/png" sizes="32x32" href="/favicon.ico?v=5.1.4">







  <meta name="keywords" content="iOS," />





  <link rel="alternate" href="/atom.xml" title="Swift社区" type="application/atom+xml" />






<meta name="description" content="前言我已经有一段时间没有从头开始一个需要支持多种语言的新项目了。当然不是从头开始，而是在代码库中通过使用 Swift 包将代码分成不同模块。 我想提醒自己记住许多在本地化实行中的过程，所以我认为最好写一篇文章，以便下次开始同类型项目时可以参考。">
<meta name="keywords" content="iOS">
<meta property="og:type" content="article">
<meta property="og:title" content="实现模块化应用的本地化">
<meta property="og:url" content="https://fanbaoying.github.io/实现模块化应用的本地化/index.html">
<meta property="og:site_name" content="Swift社区">
<meta property="og:description" content="前言我已经有一段时间没有从头开始一个需要支持多种语言的新项目了。当然不是从头开始，而是在代码库中通过使用 Swift 包将代码分成不同模块。 我想提醒自己记住许多在本地化实行中的过程，所以我认为最好写一篇文章，以便下次开始同类型项目时可以参考。">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="https://www.polpiella.dev/assets/posts/modularised-app-localisation/detail-home-en.png">
<meta property="og:image" content="https://www.polpiella.dev/assets/posts/modularised-app-localisation/default-localisation-error.png">
<meta property="og:image" content="https://www.polpiella.dev/assets/posts/modularised-app-localisation/missing-default-localisation.png">
<meta property="og:image" content="https://images.xiaozhuanlan.com/photo/2022/dfb90c005ba10bc3797eddaf4032e348.gif">
<meta property="og:image" content="https://www.polpiella.dev/assets/posts/modularised-app-localisation/enable-mixed-localizations.png">
<meta property="og:updated_time" content="2022-09-26T03:59:50.417Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="实现模块化应用的本地化">
<meta name="twitter:description" content="前言我已经有一段时间没有从头开始一个需要支持多种语言的新项目了。当然不是从头开始，而是在代码库中通过使用 Swift 包将代码分成不同模块。 我想提醒自己记住许多在本地化实行中的过程，所以我认为最好写一篇文章，以便下次开始同类型项目时可以参考。">
<meta name="twitter:image" content="https://www.polpiella.dev/assets/posts/modularised-app-localisation/detail-home-en.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://fanbaoying.github.io/实现模块化应用的本地化/"/>





  <title>实现模块化应用的本地化 | Swift社区</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>
    
    <a href="https://github.com/SwiftCommunityRes" class="github-corner" aria-label="View source on GitHub"><svg width="80" height="80" viewBox="0 0 250 250" style="fill:#151513; color:#fff; position: absolute; top: 0; border: 0; right: 0;" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a><style>.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}</style>
    
    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Swift社区</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">做最好的 Swift 社区</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://fanbaoying.github.io/实现模块化应用的本地化/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Swift社区">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="https://avatars.githubusercontent.com/u/84354365?v=4">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Swift社区">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">实现模块化应用的本地化</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2022-09-26T11:56:33+08:00">
                2022-09-26
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Swift/" itemprop="url" rel="index">
                    <span itemprop="name">Swift</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>

                <!-- 
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                -->
                <span title="字数统计">
                  数字统计 1,904字
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>

                <!-- 
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                --> 

                <span title="阅读时长">
                  阅读时长 7分钟
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h2><p>我已经有一段时间没有从头开始一个需要支持多种语言的新项目了。当然不是从头开始，而是在代码库中通过使用 Swift 包将代码分成不同模块。</p>
<p>我想提醒自己记住许多在本地化实行中的过程，所以我认为最好写一篇文章，以便下次开始同类型项目时可以参考。</p>
<a id="more"></a>
<h2 id="开始吧！"><a href="#开始吧！" class="headerlink" title="开始吧！"></a>开始吧！</h2><p>让我们看看代码库的一个简化版本。它包含一个 Xcode 项目，一个单独的 app target（即将运行的那个）和一个名为 <code>Features</code> 的 Swift 包。后者将包含 app 中所有页面的代码，每一页将被定义为自己的产品：</p>
<p><strong>Package.swift</strong></p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// swift-tools-version: 5.6</span></span><br><span class="line"><span class="keyword">import</span> PackageDescription</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> package = <span class="type">Package</span>(</span><br><span class="line">    name: <span class="string">"Features"</span>,</span><br><span class="line">    products: [</span><br><span class="line">        .library(</span><br><span class="line">            name: <span class="string">"Home"</span>,</span><br><span class="line">            targets: [<span class="string">"Home"</span>]),</span><br><span class="line">        .library(</span><br><span class="line">            name: <span class="string">"Detail"</span>,</span><br><span class="line">            targets: [<span class="string">"Detail"</span>]</span><br><span class="line">        )</span><br><span class="line">    ],</span><br><span class="line">    dependencies: [</span><br><span class="line">    ],</span><br><span class="line">    targets: [</span><br><span class="line">        .target(</span><br><span class="line">            name: <span class="string">"Home"</span></span><br><span class="line">        ),</span><br><span class="line">        .target(</span><br><span class="line">            name: <span class="string">"Detail"</span></span><br><span class="line">        )</span><br><span class="line">    ]</span><br><span class="line">)</span><br></pre></td></tr></table></figure>
<p>这个 app target 将会作为 app 的组合层，其唯一的目的是导入每个功能，实例化它们并协调导航。所有的 UI ，演示和业务逻辑将留在各自的 “模块” 中（<code>Features Swift Package</code> 中的一个 target）。这将允许每个功能独立开发并完全的与其他功能隔离。</p>
<p>为了简单起见，这个例子里仅有两个功能：主页和详情，他们代表 app 中仅有的两个页面。</p>
<p>主页有一个按钮允许用户导航到详情页面，还有一个标签展示用户当前所在区域的语言代码。详情页只展示一个标签，和主页标签展示的信息一致：</p>
<p><img src="https://www.polpiella.dev/assets/posts/modularised-app-localisation/detail-home-en.png" alt="Images of both screens with english selected as the language"></p>
<h2 id="添加字符串！"><a href="#添加字符串！" class="headerlink" title="添加字符串！"></a>添加字符串！</h2><p>看起来不错，但是现在展示的信息是用英文通过硬编码编写的字符串。app 需要内容被翻译成另外两种语言：加泰罗尼亚语和西班牙语。</p>
<p>虽然有多种实现方式，我更倾向每个功能（或页面）只包含它所需要的本地化字符串，这样可以增加功能的可移植性和可重用性。</p>
<p>这可以在 Swift 包中完成，通过将所有必需的 <code>.lproj</code> 文件和所有需要本地化的内容（当前例子中只有 <code>Localizable.strings</code> 文件）放在目标文件夹下 - 我的习惯是放在父 <code>Resources/</code> 文件夹下，并将这些资源定义为 <code>Package.swift</code> 的特定 target。</p>
<p>添加文件之后构建该功能将导致编译器抛出如下错误：</p>
<p><img src="https://www.polpiella.dev/assets/posts/modularised-app-localisation/default-localisation-error.png" alt="Error thrown by Xcode when no default localisation is set"></p>
<p>这是因为 <code>defaultLocalization</code> 必须由 <code>Package.swift</code> 提供。所有功能的 target 来自一个包，所以只能有一个 <code>defaultLocalization</code> 。以下是 <code>Package.swift</code> 添加本地化内容之后的样子：</p>
<p><strong>Package.swift</strong></p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// swift-tools-version: 5.6</span></span><br><span class="line"><span class="keyword">import</span> PackageDescription</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> package = <span class="type">Package</span>(</span><br><span class="line">    name: <span class="string">"Features"</span>,</span><br><span class="line">    defaultLocalization: <span class="string">"en"</span>,</span><br><span class="line">    platforms: [.iOS(.v15)],</span><br><span class="line">    products: [</span><br><span class="line">        .library(</span><br><span class="line">            name: <span class="string">"Home"</span>,</span><br><span class="line">            targets: [<span class="string">"Home"</span>]),</span><br><span class="line">        .library(</span><br><span class="line">            name: <span class="string">"Detail"</span>,</span><br><span class="line">            targets: [<span class="string">"Detail"</span>]</span><br><span class="line">        )</span><br><span class="line">    ],</span><br><span class="line">    dependencies: [</span><br><span class="line">    ],</span><br><span class="line">    targets: [</span><br><span class="line">        .target(</span><br><span class="line">            name: <span class="string">"Home"</span>,</span><br><span class="line">            dependencies: [],</span><br><span class="line">            resources: [.process(<span class="string">"Resources/"</span>)]</span><br><span class="line">        ),</span><br><span class="line">        .target(</span><br><span class="line">            name: <span class="string">"Detail"</span>,</span><br><span class="line">            resources: [.process(<span class="string">"Resources/"</span>)]</span><br><span class="line">        )</span><br><span class="line">    ]</span><br><span class="line">)</span><br></pre></td></tr></table></figure>
<blockquote>
<p>注意：如果没有为默认的本地化代码提供本地化的内容，编译器会显示警告。这对于确保你不会发布包含基本本地化内容的软件包版本非常有帮助。</p>
<p><img src="https://www.polpiella.dev/assets/posts/modularised-app-localisation/missing-default-localisation.png" alt="Xcode warning shown when default localisation is missing."></p>
</blockquote>
<h2 id="支持本地化"><a href="#支持本地化" class="headerlink" title="支持本地化"></a>支持本地化</h2><p>可能与你的想法正好相反，把设备系统语言设置为加泰罗尼亚语或西班牙语并且运行 app 内容仍然用英文展示。原因是 Swift 包需要额外的信息去决定使用哪些本地化的内容，就目前来看，如果包里有目标内容，它们将只使用目标的基本本地化，否则使用包的默认本地化。</p>
<p>现在有两种方式我们可以实现本地化：使新的本地化在 app target 中可用或启用混合本地化。</p>
<h3 id="在-app-target-中添加新的本地化内容"><a href="#在-app-target-中添加新的本地化内容" class="headerlink" title="在 app target 中添加新的本地化内容"></a>在 app target 中添加新的本地化内容</h3><p>在 <code>Features</code>  Swift 包中启用新的本地化的一种方式是将它们添加到导入功能的 Xcode 项目中。这可以通过进入 Xcode 项目，在项目设置中的 “Info” 一栏，添加本地化支持：</p>
<p><img src="https://images.xiaozhuanlan.com/photo/2022/dfb90c005ba10bc3797eddaf4032e348.gif" alt=""></p>
<blockquote>
<p>需要注意的是，本地化需要至少一个文件（例如一个空的 <code>Localizable.strings</code> 文件）。在本例中，因为 app target 是用 UIKit 构建的，并且在添加新的本地化时选择了启动 storyboard 进行本地化（如上视频所示），所以已经有一个本地化文件。</p>
</blockquote>
<p>现在这将允许包从主包中获取支持的本地化，并选择相应的要使用的资源。</p>
<p>值得注意的是，如果设备有被 app 支持但是包不支持的语言，则后者将会回退到 <code>Package.swift</code> 中提供的 <code>defaultLocalization</code> .</p>
<p>同样的，如果 app 不支持该语言，同样会回退到相同的值。这也是为什么将 <code>defaultLocalization</code> 设置为与主目标基础语言相同，以确保所有页面上的一致性是非常重要的。这也是我更倾向于所有功能分组在一个 Swift 包之下的原因，这样所有页面上的 <code>defaultLocalization</code> 就有了单一真正的来源。</p>
<h3 id="允许混合本地化"><a href="#允许混合本地化" class="headerlink" title="允许混合本地化"></a>允许混合本地化</h3><p>虽然采用 app target 的本地化是首选方法，因为他确保了所有页面的一致性，并且只允许少数受支持的地方使用，但还有另一种方法允许包内容被本地化，而不必在主项目之外。</p>
<p>可以通过将 app 的 <code>Info.plist</code> 文件中的 <code>CFBundleAllowMixedLocalizations</code> 值设置为 <code>YES</code> 来实现。</p>
<p>这个设置将会告诉 app target 在不同的 target 或功能使用不同本地化是可以的，当添加新的本地化资源时， app 本地化会自动工作。</p>
<p><img src="https://www.polpiella.dev/assets/posts/modularised-app-localisation/enable-mixed-localizations.png" alt="Enabling mixed localisations in the app target"></p>
<p>使用这种方法需要注意以下几点：</p>
<p>1.不再需要将本地化添加到 app target，添加带有本地化内容的 <code>lproj</code> 到包资源就可以了。当用户修改区域时，如果你的资源包存在该语言包或默认提供 <code>Package.swift</code> ，软件包也会展示该区域的语言内容。</p>
<p>2.支持多少个区域就会有多少个本地化资源。这意味着没有一个单一的真实来源来确定整个 app 支持哪些本地化。这可能会导致一些问题，例如，某个功能有本地化资源内容，而该内容的本地化资源还未被应用。在本例中，除了删除资源，没有办法隐藏它。</p>
<video autoplay loop><br>  <source src="https://www.polpiella.dev/assets/posts/modularised-app-localisation/mixed-localisations.mp4" type="video/mp4"><br></video>

<p>第二点如上面的视频中所示，当用户把设备语言设置为法语。混合来源导致了不一致，因为主屏幕没有 <code>fr.lproj</code> –因此它又回到了默认本地化资源，英语。另一方面，在详情页面，有可用的本地化内容，这是正确翻译字符串的原因，正是这个原因，我喜欢将 app target 作为所有支持本地化的真实来源。</p>
<h2 id="额外提示-自动化"><a href="#额外提示-自动化" class="headerlink" title="额外提示 - 自动化"></a>额外提示 - 自动化</h2><p>我一直鼓励尽可能地自动化检索特定包的本地化字符串的流程。如果你的 app 有很多页面，希望使添加本地化字符串的过程尽可能简单和简便。</p>
<p>我一直在使用的一款工具 <a href="https://github.com/SwiftGen/SwiftGen" target="_blank" rel="noopener">SwiftGen</a>，它可以为各种资源生成 Swift 接口，例如 <code>Localizable.strings</code> 文件。</p>
<p>创建一个利用这个可执行文件的构建工具插件，可以使支持新本地化过程变得容易一点，并在各功能之间保持一致。</p>

      
    </div>
    
    
    
    

    
      <div>
        <div>
    
        <div style="text-align:center;color: #ccc;font-size:14px;">-------------本文结束<i class="fa fa-paw"></i>感谢您的阅读-------------</div>
    
</div>

      </div>
    


    
      <div>
        
<div class="my_post_copyright">
  <script src="//cdn.bootcss.com/clipboard.js/1.5.10/clipboard.min.js"></script>

  <!-- JS库 sweetalert 可修改路径 -->
  <script src="https://cdn.bootcss.com/jquery/2.0.0/jquery.min.js"></script>
  <script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
  <p><span>本文标题:</span><a href="/实现模块化应用的本地化/">实现模块化应用的本地化</a></p>
  <p><span>文章作者:</span><a href="/" title="访问 Swift社区 的个人博客">Swift社区</a></p>
  <p><span>发布时间:</span>2022年09月26日 - 11:09</p>
  <p><span>最后更新:</span>2022年09月26日 - 11:09</p>
  <p><span>原始链接:</span><a href="/实现模块化应用的本地化/" title="实现模块化应用的本地化">https://fanbaoying.github.io/实现模块化应用的本地化/</a>
    <span class="copy-path"  title="点击复制文章链接"><i class="fa fa-clipboard" data-clipboard-text="https://fanbaoying.github.io/实现模块化应用的本地化/"  aria-label="复制成功！"></i></span>
  </p>
  <p><span>许可协议:</span><i class="fa fa-creative-commons"></i> <a rel="license" href="https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank" title="Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)">署名-非商业性使用-禁止演绎 4.0 国际</a> 转载请保留原文链接及作者。</p>  
</div>
<script> 
    var clipboard = new Clipboard('.fa-clipboard');
      $(".fa-clipboard").click(function(){
      clipboard.on('success', function(){
        swal({   
          title: "",   
          text: '复制成功',
          icon: "success", 
          showConfirmButton: true
          });
        });
    });  
</script>

      </div>
    


    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>坚持原创技术分享，您的支持将鼓励我继续创作！</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="/images/wechat.png" alt="Swift社区 微信支付"/>
        <p>微信支付</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="/images/alipay.png" alt="Swift社区 支付宝"/>
        <p>支付宝</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/iOS/" rel="tag"><i class="fa fa-tag"></i> iOS</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/SwiftUI-之-HStack-和-VStack-的切换/" rel="next" title="SwiftUI 之 HStack 和 VStack 的切换">
                <i class="fa fa-chevron-left"></i> SwiftUI 之 HStack 和 VStack 的切换
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/Swift-周报-第十四期/" rel="prev" title="Swift 周报 第十四期">
                Swift 周报 第十四期 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
        <!-- JiaThis Button BEGIN -->
<div class="jiathis_style">
<span class="jiathis_txt">分享到：</span>
<a class="jiathis_button_fav">收藏夹</a>
<a class="jiathis_button_copy">复制网址</a>
<a class="jiathis_button_email">邮件</a>
<a class="jiathis_button_weixin">微信</a>
<a class="jiathis_button_qzone">QQ空间</a>
<a class="jiathis_button_tqq">腾讯微博</a>
<a class="jiathis_button_douban">豆瓣</a>
<a class="jiathis_button_share">一键分享</a>

<a href="http://www.jiathis.com/share?uid=2140465" class="jiathis jiathis_txt jiathis_separator jtico jtico_jiathis" target="_blank">更多</a>
<a class="jiathis_counter_style"></a>
</div>
<script type="text/javascript" >
var jiathis_config={
  data_track_clickback:true,
  summary:"",
  shortUrl:false,
  hideMore:false
}
</script>
<script type="text/javascript" src="http://v3.jiathis.com/code/jia.js?uid=" charset="utf-8"></script>
<!-- JiaThis Button END -->
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="https://avatars.githubusercontent.com/u/84354365?v=4"
                alt="Swift社区" />
            
              <p class="site-author-name" itemprop="name">Swift社区</p>
              <p class="site-description motion-element" itemprop="description">我们的使命是做一个最专业最权威的 Swift 中文社区，我们的愿景是希望更多的人学习和使用Swift。我们会分享以 Swift 实战、SwiftUI、Swift 基础为核心的技术干货，不忘初心，牢记使命。</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">187</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">19</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">11</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          
            <div class="feed-link motion-element">
              <a href="/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://blog.csdn.net/qq_36478920" target="_blank" title="CSDN">
                      
                        <i class="fa fa-fw fa-heartbeat"></i>CSDN</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://juejin.im/user/5a30d987f265da430d580126" target="_blank" title="掘金">
                      
                        <i class="fa fa-fw fa-spinner"></i>掘金</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/fanbaoying" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://www.zhihu.com/people/swift-community" target="_blank" title="知乎">
                      
                        <i class="fa fa-fw fa-heartbeat"></i>知乎</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://weibo.com/u/7711465033" target="_blank" title="微博">
                      
                        <i class="fa fa-fw fa-weibo"></i>微博</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://mp.weixin.qq.com/mp/appmsgalbum?__biz=MzAxNzgzNTgwMw==&action=getalbum&album_id=2024314764775981057" target="_blank" title="公众号">
                      
                        <i class="fa fa-fw fa-heartbeat"></i>公众号</a>
                  </span>
                
            </div>
          

          
          

          
          
            <div class="links-of-blogroll motion-element links-of-blogroll-block">
              <div class="links-of-blogroll-title">
                <i class="fa  fa-fw fa-link"></i>
                友情链接
              </div>
              <ul class="links-of-blogroll-list">
                
                  <li class="links-of-blogroll-item">
                    <a href="https://github.com/SwiftCommunityRes/SwiftWeekly" title="SwiftWeekly" target="_blank">SwiftWeekly</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://github.com/SwiftCommunityRes/article-ios" title="Article-ios" target="_blank">Article-ios</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://github.com/SwiftCommunityRes/SwiftUI-Book" title="SwiftUI-Book" target="_blank">SwiftUI-Book</a>
                  </li>
                
              </ul>
            </div>
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#前言"><span class="nav-number">1.</span> <span class="nav-text">前言</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#开始吧！"><span class="nav-number">2.</span> <span class="nav-text">开始吧！</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#添加字符串！"><span class="nav-number">3.</span> <span class="nav-text">添加字符串！</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#支持本地化"><span class="nav-number">4.</span> <span class="nav-text">支持本地化</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#在-app-target-中添加新的本地化内容"><span class="nav-number">4.1.</span> <span class="nav-text">在 app target 中添加新的本地化内容</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#允许混合本地化"><span class="nav-number">4.2.</span> <span class="nav-text">允许混合本地化</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#额外提示-自动化"><span class="nav-number">5.</span> <span class="nav-text">额外提示 - 自动化</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        
<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<div class="copyright">&copy; <span itemprop="copyrightYear">2024</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Swift社区</span>

  
</div>

<!-- 
<div class="powered-by">
  <i class="fa fa-user-md">
  </i>
  <span id="busuanzi_container_site_uv">
    已有<span id="busuanzi_value_site_uv"></span>人来访
  </span>

</div>


  <span class="post-meta-divider">|</span>


<div class="powered-by">
  <i class="fa fa-eye"></i>
  <span id="busuanzi_container_site_pv">
      访问量<span id="busuanzi_value_site_pv"></span>次
  </span>
</div>



  <span class="post-meta-divider">|</span>


-->

<div class="theme-info">
  <i class="fa fa-pencil"></i>
  <span class="post-count">博客全站共361.6k字</span>
</div>


<!-- 
  <span class="post-meta-divider">|</span>



  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Pisces</a> v5.1.4</div>



-->

        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
